package cn.schoolwow.ams.test.module.flowLog.aware;

import cn.schoolwow.ams.domain.block.common.operation.CustomerMessage;
import cn.schoolwow.ams.domain.block.common.operation.ListBlockHandlerBlockOperation;
import cn.schoolwow.ams.domain.block.common.operation.OpenFieldTabBlockOperation;
import cn.schoolwow.ams.domain.block.list.common.ListBlockButtonType;
import cn.schoolwow.ams.domain.block.list.common.ListBlockOperationHandler;
import cn.schoolwow.ams.domain.block.list.dao.crud.DatabaseListBlockListListener;
import cn.schoolwow.ams.domain.block.list.dao.crud.DatabaseListBlockTableField;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockConfigAware;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockOption;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.BlockOperationPosition;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.ListBlockRequest;
import cn.schoolwow.ams.test.entity.FlowLog;
import cn.schoolwow.ams.test.module.common.ExecuteFlowLogFlow;
import cn.schoolwow.ams.test.module.flowLog.flow.business.UserLoginExceptionFlow;
import cn.schoolwow.ams.test.module.flowLog.flow.business.UserLoginFlow;
import cn.schoolwow.quickdao.domain.database.dql.condition.Condition;
import cn.schoolwow.quickdao.domain.database.dql.response.PageVo;
import cn.schoolwow.quickflow.QuickFlow;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class FlowLogAware extends EntityTableListBlockConfigAware {
    @Autowired
    private QuickFlow quickFlow;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public EntityTableListBlockOption getAMSBlockConfig() {
        EntityTableListBlockOption entityTableListBlockOption = new EntityTableListBlockOption(
                "test","流程日志", "starterDAO", FlowLog.class);
        {
            entityTableListBlockOption.listBlockShowOption.displayFieldNames = new String[]{"id","flowId", "name", "sql", "brokenReason", "exceptionMessage", "consumeTime", "createdAt"};
            entityTableListBlockOption.listBlockShowOption.initialSortField = "id";
            entityTableListBlockOption.listBlockShowOption.initialSortOrder = "desc";
            entityTableListBlockOption.listBlockShowOption.requestFieldNames = new String[]{"id","flowId", "name"};
        }
        entityTableListBlockOption.listBlockShowOption.displayFieldNames = new String[]{
                "createdAt","flowId","name","brokenReason","exceptionMessage","consumeTime"
        };
        {
            entityTableListBlockOption.listBlockShowOption.listBlockButtonTypeArray = new ListBlockButtonType[]{
                    ListBlockButtonType.CONDITION_DELETE,
                    ListBlockButtonType.ALL_DELETE
            };
        }
        setListListener(entityTableListBlockOption);
        addButton(entityTableListBlockOption);
        return entityTableListBlockOption;
    }

    private void setListListener(EntityTableListBlockOption entityTableListBlockOption){
        entityTableListBlockOption.databaseListBlockListener.listListener = new DatabaseListBlockListListener() {
            @Override
            public void beforeExecuteCondition(Condition condition, JSONObject queryCondition) throws Exception {
                Integer logType = queryCondition.getInteger("logType");
                if(null!=logType){
                    switch (logType){
                        case 1:{
                            condition.addNullQuery("brokenReason")
                                    .addNullQuery("exception");
                        }break;
                        case 2:{
                            condition.addNotNullQuery("exception");
                        }break;
                        case 3:{
                            condition.addNotNullQuery("brokenReason");
                        }break;
                    }
                }
            }

            @Override
            public void afterExecuteCondition(Condition condition, JSONObject queryCondition, PageVo pageVo) throws Exception {

            }

            @Override
            public DatabaseListBlockTableField[] listTableFieldArray() {
                return new DatabaseListBlockTableField[]{
                        new DatabaseListBlockTableField("logType", "日志类型(0:全部,1:正常日志,2:异常日志,3:中断日志)", "select", true)
                };
            }
        };
    }

    private void addButton(EntityTableListBlockOption entityTableListBlockOption){
        {
            ListBlockHandlerBlockOperation listBlockHandlerBlockOperation = new ListBlockHandlerBlockOperation("添加流程日志", new ListBlockOperationHandler() {
                @Override
                public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                    quickFlow.startFlow(applicationContext.getBean(UserLoginFlow.class))
                            .putCurrentCompositeFlowData("username", "ams")
                            .putCurrentCompositeFlowData("password", "123456")
                            .execute();
                    return null;
                }
            });
            listBlockHandlerBlockOperation.position = BlockOperationPosition.RightTop;
            entityTableListBlockOption.blockOperationList.add(listBlockHandlerBlockOperation);
        }
        {
            ListBlockHandlerBlockOperation listBlockHandlerBlockOperation = new ListBlockHandlerBlockOperation("添加异常流程日志", new ListBlockOperationHandler() {
                @Override
                public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                    quickFlow.startFlow(applicationContext.getBean(UserLoginExceptionFlow.class))
                            .execute();
                    return null;
                }
            });
            listBlockHandlerBlockOperation.position = BlockOperationPosition.RightTop;
            entityTableListBlockOption.blockOperationList.add(listBlockHandlerBlockOperation);
        }
        {
            ListBlockHandlerBlockOperation listBlockHandlerBlockOperation = new ListBlockHandlerBlockOperation("执行流程", new ListBlockOperationHandler() {
                @Override
                public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                    quickFlow.startFlow(applicationContext.getBean(ExecuteFlowLogFlow.class))
                            .putTemporaryInstanceData(listBlockRequest)
                            .execute();
                    return null;
                }
            });
            entityTableListBlockOption.blockOperationList.add(listBlockHandlerBlockOperation);
        }
        {
            OpenFieldTabBlockOperation openFieldTabBlockOperation = new OpenFieldTabBlockOperation("查看详情",
                    Arrays.asList("flowTrace", "sql", "exception", "requestData", "contextData", "quickFlowData", "flowConfigData"));
            entityTableListBlockOption.blockOperationList.add(openFieldTabBlockOperation);
        }
    }

}
